#include "dominion.h"
#include <stdio.h>
#include "rngs.h"
#include <stdlib.h>
#include <assert.h>

int checkSmithy(int prevNum, int currNum) {

  assert(prevNum + 2 == currNum);
}

int main (int argc, char** argv) {
  for (int i =0; i<2000; i++) {
  int randomN = rand() % 1000;
  int randomP = rand() % 4;
  struct gameState G;
  int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, 
           sea_hag, tribute, smithy};
  
  printf ("Starting game.\n");
  
  initializeGame(randomP, k, randomN, &G);
  
  int money = 0;
  int smithyPos = -1;
  int adventurerPos = -1;
  int i=0;

  int numSmithies = 0;
  int numAdventurers = 0;

  while (!isGameOver(&G)) {
    money = 0;
    smithyPos = -1;
    adventurerPos = -1;
    for (i = 0; i < numHandCards(&G); i++) {
      if (handCard(i, &G) == copper)
    money++;
      else if (handCard(i, &G) == silver)
    money += 2;
      else if (handCard(i, &G) == gold)
    money += 3;
      else if (handCard(i, &G) == smithy)
    smithyPos = i;
      else if (handCard(i, &G) == adventurer)
    adventurerPos = i;
    }

    if (whoseTurn(&G) == 0) {
      if (smithyPos != -1) {
        printf("0: smithy played from position %d\n", smithyPos); 
	int prevNum = numHandCards(&G);
        playCard(smithyPos, -1, -1, -1, &G); 
	int currNum = numHandCards(&G);
	checkSmithy(prevNum, currNum);
        printf("smithy played.\n");
	money = 0;
	i=0;
	while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if (money >= 8) {
        printf("0: bought province\n"); 
        buyCard(province, &G);
      }
      else if (money >= 6) {
        printf("0: bought gold\n"); 
        buyCard(gold, &G);
      }
      else if ((money >= 4) && (numSmithies < 2)) {
        printf("0: bought smithy\n"); 
        buyCard(smithy, &G);
        numSmithies++;
      }
      else if (money >= 3) {
        printf("0: bought silver\n"); 
        buyCard(silver, &G);
      }

      printf("0: end turn\n");
      endTurn(&G);
    }
    else if (whoseTurn(&G) == 1) {
      if (adventurerPos != -1) {
        printf("1: adventurer played from position %d\n", adventurerPos);	
        playCard(adventurerPos, -1, -1, -1, &G);
	int currentPlayer = whoseTurn(&G);
        int drawnTreasure1 = handCard(numHandCards(&G)-1, &G);
        int drawnTreasure2 = handCard(numHandCards(&G)-2, &G);
        assert(drawnTreasure1 == copper || drawnTreasure1 == silver || drawnTreasure1 == gold);
	assert(drawnTreasure2 == copper || drawnTreasure2 == silver || drawnTreasure2 == gold);
        money = 0;
	i=0;
	
        while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;         
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if (money >= 8) {
        printf("1: bought province\n");
        buyCard(province, &G);
      }
      else if ((money >= 6) && (numAdventurers < 2)) {
        printf("1: bought adventurer\n");
	buyCard(adventurer, &G);
	numAdventurers++;
      }else if (money >= 6){
        printf("1: bought gold\n");
	    buyCard(gold, &G);
        }
      else if (money >= 3){
        printf("1: bought silver\n");
	    buyCard(silver, &G);
      }
      printf("1: endTurn\n");
      
      endTurn(&G);      
    }
  else if (whoseTurn(&G) == 2) {
if (smithyPos != -1) {
        printf("0: smithy played from position %d\n", smithyPos); 
	int prevNum = numHandCards(&G);
        playCard(smithyPos, -1, -1, -1, &G); 
	int currNum = numHandCards(&G);
	checkSmithy(prevNum, currNum);
        printf("smithy played.\n");
	money = 0;
	i=0;
	while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if (money >= 8) {
        printf("2: bought province\n"); 
        buyCard(province, &G);
      }
      else if (money >= 6) {
        printf("2: bought gold\n"); 
        buyCard(gold, &G);
      }
      else if ((money >= 4) && (numSmithies < 2)) {
        printf("2: bought smithy\n"); 
        buyCard(smithy, &G);
        numSmithies++;
      }
      else if (money >= 3) {
        printf("2: bought silver\n"); 
        buyCard(silver, &G);
      }

      printf("2: end turn\n");
      endTurn(&G);
}
else if (whoseTurn(&G) ==3) {
if (adventurerPos != -1) {
        printf("3: adventurer played from position %d\n", adventurerPos);	
        playCard(adventurerPos, -1, -1, -1, &G);
	int currentPlayer = whoseTurn(&G);
        int drawnTreasure1 = handCard(numHandCards(&G)-1, &G);
        int drawnTreasure2 = handCard(numHandCards(&G)-2, &G);
        assert(drawnTreasure1 == copper || drawnTreasure1 == silver || drawnTreasure1 == gold);
	assert(drawnTreasure2 == copper || drawnTreasure2 == silver || drawnTreasure2 == gold);
        money = 0;
	i=0;
	
        while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;         
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if (money >= 8) {
        printf("3: bought province\n");
        buyCard(province, &G);
      }
      else if ((money >= 6) && (numAdventurers < 2)) {
        printf("3: bought adventurer\n");
	buyCard(adventurer, &G);
	numAdventurers++;
      }else if (money >= 6){
        printf("3: bought gold\n");
	    buyCard(gold, &G);
        }
      else if (money >= 3){
        printf("3: bought silver\n");
	    buyCard(silver, &G);
      }
      printf("3: endTurn\n");
      
      endTurn(&G);      


}
  } // end of While

  printf ("Finished game.\n");
  /*if (randomP == 0) {
  printf ("Player 0: %d\n", scoreFor(0, &G));

  }
  if (randomP == 1) {
printf ("Player 0: %d\nPlayer 1: %d\n", scoreFor(0, &G), scoreFor(1, &G));

  }
  if (randomP == 2) {
printf ("Player 0: %d\nPlayer 1: %d\nPlayer 2: %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(2, &G));

  }
  if (randomP == 3) {
  printf ("Player 0: %d\nPlayer 1: %d\nPlayer 2: %d\nPlayer 3: %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(2, &G), scoreFor(3, &G));
  } */
  }
  return 0;
}
